package net.mihome.app.adapter;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import net.mihome.app.R;
import net.mihome.app.bean.Community;

import java.util.List;

/**
 * Created by ZHAO on 2015/1/12 0012.
 */
public class SeachCommunityAdapter extends BaseAdapter{
    Activity context;
    List<Community> users;
    private LayoutInflater listContainer;
    private ProgressDialog progressDialog;
    public SeachCommunityAdapter(Context context, List<Community> list){
        this.context= (Activity) context;
        this.users=list;
        listContainer = LayoutInflater.from(context);
    }
    public static class ViewContaiar{
        public ImageView avatar;
        public TextView name;
        public Button addContact;
        public Community community;
    }
    @Override
    public int getCount() {
        return users.size();
    }

    @Override
    public Object getItem(int i) {
        return users.get(i);
    }

    @Override
    public long getItemId(int i) {
        return users.get(i).getId();
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {

        ViewContaiar viewContaiar=null;
        if(view==null){
            viewContaiar=new ViewContaiar();
            view = listContainer.inflate(R.layout.search_community_listitem, viewGroup, false);
            viewContaiar.avatar= (ImageView) view.findViewById(R.id.community_avatar);
            viewContaiar.name= (TextView) view.findViewById(R.id.community_name);
            viewContaiar.addContact= (Button) view.findViewById(R.id.community_indicator);
            view.setTag(viewContaiar);
        }else {
            viewContaiar= (ViewContaiar) view.getTag();
        }

        Community user=this.users.get(i);
        viewContaiar.community=user;
        if(user!=null){
            viewContaiar.name.setText(user.getName());
            viewContaiar.addContact.setTag(user);
//            setOnClickLinster(viewContaiar.addContact);

        }else{
            return view;
        }
        return view;
    }
//    public void setOnClickLinster(Button button){
//        button.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                final Community user= (Community) view.getTag();
//                User curUser=AppContext.getInstance().getLoginInfo();
//
//                String curName= curUser.getName();
//                String inputName=user.getName();
//                if(curName.equals(inputName)){
//                    context.startActivity(new Intent(context, AlertDialog.class).putExtra("msg", "不能添加自己"));
//                    return;
//                }
//
//                if(AppContext.getInstance().getContactList().containsKey(inputName)){
//                    context.startActivity(new Intent(context, AlertDialog.class).putExtra("msg", "此用户已是你的好友"));
//                    return;
//                }
//                showAddMsg(user);
//
//            }
//        });
//    }
//    /**
//     * 显示一个对话框，让用户填写好友验证
//     * */
//    public void showAddMsg(final User user){
//        LayoutInflater inflater = LayoutInflater.from(context);
//        View view = inflater.inflate(R.layout.add_contact_dialog, null);//这里的
//        final EditText editText= (EditText) view.findViewById(R.id.add_contact_msg);
//        android.app.AlertDialog.Builder builder = new android.app.AlertDialog.Builder(context);
//        builder.setTitle("请输入验证信息:");
//        builder.setMessage("你想对他(她)说点什么?");
//        builder.setView(view);
//        builder.setPositiveButton(R.string.text_queding, new DialogInterface.OnClickListener() {
//            @Override
//            public void onClick(DialogInterface dialogInterface, int i) {
//                Toast.makeText(context,"确定",Toast.LENGTH_SHORT).show();
//                dialogInterface.dismiss();
//
//                addContact(user,editText.getText().toString());
//            }
//        });
//        builder.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
//            @Override
//            public void onClick(DialogInterface dialogInterface, int i) {
//                dialogInterface.dismiss();
//            }
//        });
//        builder.create().show();
//    }
//    /**
//     * 启动线程添加好友
//     * */
//    public void addContact(final User user,final String msg){
//                progressDialog = new ProgressDialog(context);
//                progressDialog.setMessage("正在发送请求...");
//                progressDialog.setCanceledOnTouchOutside(false);
//                progressDialog.show();
//
//                new Thread(new Runnable() {
//                    public void run() {
//                        try {
//                            //demo写死了个reason，实际应该让用户手动填入
//                            EMContactManager.getInstance().addContact(user.getOpenId(), "验证信息:"+msg);
//                            context.runOnUiThread(new Runnable() {
//                                public void run() {
//                                    progressDialog.dismiss();
//                                    Toast.makeText(context, "发送请求成功,等待对方验证", Toast.LENGTH_LONG).show();
//                                }
//                            });
//                        } catch (final Exception e) {
//                            context.runOnUiThread(new Runnable() {
//                                public void run() {
//                                    progressDialog.dismiss();
//                                    Toast.makeText(context, "请求添加好友失败:" + e.getMessage(), Toast.LENGTH_LONG).show();
//                                }
//                            });
//                        }
//                    }
//                }).start();
//    }
  }
